package com.yyw.gulimall.member.controller;

import com.yyw.common.utils.PageUtils;
import com.yyw.common.utils.R;
import com.yyw.common.utils.enums.ExceptionEnum;
import com.yyw.gulimall.member.entity.MemberEntity;
import com.yyw.gulimall.member.feign.CouponFeiogn;
import com.yyw.gulimall.member.service.MemberService;
import com.yyw.gulimall.member.vo.LoginUserVo;
import com.yyw.common.to.oauth.MemberVo;
import com.yyw.gulimall.member.vo.weibo.AccessTokenResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 会员
 *
 * @author yiyw
 * @date 2020-10-24 12:09:19
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeiogn couponFeiogn;

    @GetMapping("/tets/feign")
    public R testFeign() {
        R list = couponFeiogn.list(new HashMap<>());
        return R.ok().put("resurt", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 用户注册
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {

        try {
            memberService.registerUser(member);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(ExceptionEnum.USER_EXCEPTION.getCode(), e.getMessage());
        }

        return R.ok();
    }

    /**
     * 用户登录
     *
     * @param userVo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginUserVo userVo) {
        MemberVo memberVo = null;
        try {
            memberVo = memberService.loginUser(userVo);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(ExceptionEnum.USER_LOGIN_EXCEPTION.getCode(), ExceptionEnum.USER_LOGIN_EXCEPTION.getMsg());
        }
        return R.ok().put("data", memberVo);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 第三方登录 /微博/QQ...
     *
     * @param tokenResult 请求登录的数据
     * @param source      来源 QQ/微博
     * @return
     */
    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody AccessTokenResult tokenResult, @RequestParam String source) {
        try {
            MemberVo memberVo = memberService.oauthLoginOrRegister(tokenResult, source);
            return R.ok().put("data", memberVo);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(ExceptionEnum.USER_EXCEPTION.getCode(), e.getMessage());
        }
    }

}
